# coding:utf-8

from flask import Flask
from flask import request
from flask import render_template

app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def home():
    return '<h1>Fuck you!</h1>'

@app.route('/signin', methods=['GET'])
def signin_form():
    return '''<form action="/signin" method="post">
              <p><input name="username"></p>
              <p><input name="password" type="password"></p>
              <p><button type="submit">Sign In</button></p>
              </form>'''
    
@app.route('/signin', methods=['POST'])
def signin():
    # 读取表单参数
    if request.form['username'] == 'admin' and request.form['password'] == '123456':
        return '<h1>Hello World</h1>'
    else:
        return '<h1>What a stupid man</h1>'
    
    
'''使用 jinja2模版 ， 一定要把模板放到正确的templates目录下，templates和app.py在同级目录下'''

@app.route('/test', methods=['GET'])
def test():
    # 返回模版中的html
    return render_template('test.html',list=['a','b','c'])

@app.route('/login', methods=['GET'])
def login_form():
    return render_template('login.html')

@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    if username == 'admin' and password == '123456':
        return render_template('login_result.html', username=username)
    else :
        return render_template('login_result.html')

if __name__ == '__main__':
    app.run('192.168.1.38', '5000')
